package com.horoscope.astrology.zodiac.signs.free.utils

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.share.Sharer
import com.facebook.share.model.ShareLinkContent
import com.facebook.share.widget.ShareDialog
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R

object ShareManager {
    private val TAG = ShareManager::class.java.simpleName

    fun initShare(
        context: Activity,
        line_left: View?,
        line_right: View?,
        text_view_share: TextView?,
        image_view_more_share: ImageView,
        tv_facebook: TextView
    ) {
        // 一些背景为白色的 单独设置
        if (line_left != null) {
            line_left.setBackgroundColor(ContextCompat.getColor(context, R.color.def_gray))
            line_right!!.setBackgroundColor(ContextCompat.getColor(context, R.color.def_gray))
            text_view_share!!.setTextColor(Color.BLACK)
        }

        image_view_more_share.setOnClickListener {
            sysCallsShare(
                context,
                MmoConstant.URL_RATING_HTTP
            )
        }
        tv_facebook.setOnClickListener {
            shareToFacebook(
                context,
                MmoConstant.URL_RATING_HTTP
            )
        }

    }


    fun getFacebookCallbackManager(): CallbackManager? {
        return facebookCallbackManager
    }

    private var facebookCallbackManager: CallbackManager? = null
    fun shareToFacebook(activity: Activity, url: String) {
        if (SysUtils.isInstallFacebook(activity as Context)) {
            sysCallShareFB(activity, url)
        } else {
            fbAPIShare(activity, url)
        }

    }


    private fun checkFB(activity: Activity): Boolean {
        val info = ShareU.getShareApps(activity)
        info.forEach {
            Log.d(
                "HRHERR",
                "" + "appName = ${it.appName},launcherName ${it.launcherName}, packageName:${it.packageName}"
            )
            if (it.packageName == "com.facebook.katana") {
                return true
            }
        }
        return false
    }

    private fun fbAPIShare(activity: Activity, url: String) {
        //系统的不好用，有可能调不起来，而且体验不好，慎重用
        facebookCallbackManager = CallbackManager.Factory.create()
        val facebookShareDialog = ShareDialog(activity)
        facebookShareDialog.registerCallback(facebookCallbackManager,
            object : FacebookCallback<Sharer.Result> {
                override fun onCancel() {
                    Log.d(TAG, "ShareToFacebook --onCancel")
                }

                override fun onError(error: FacebookException?) {
                    Log.d(TAG, "ShareToFacebook --error:$error")
                }

                override fun onSuccess(result: Sharer.Result?) {
                    Log.d(TAG, "ShareToFacebook --success")
                    //ToastUtils.showLongToast(activity, R.string.toast_share_success)
                }

            })
        val linkContent = ShareLinkContent.Builder().setContentUrl(Uri.parse(url)).build()
        if (ShareDialog.canShow(ShareLinkContent::class.java)) {
            facebookShareDialog.show(linkContent)
        }
    }


    /*fun shareToTwitter(activity: Activity, url: String) {
        var intent = Intent()
        try {
            intent = TweetComposer.Builder(activity)
                .text("")
                .url(URL(url))
                //.image()
                .createIntent()
        } catch (e: MalformedURLException) {
            e.printStackTrace()
        }
        activity.startActivityForResult(intent, AppConstants.REQUEST_CODE_TWITTER)
    }*/

    fun sysCallsShare(activity: Activity, url: String) {
        try {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "text/plain"
            intent.putExtra(Intent.EXTRA_SUBJECT, "")
            intent.putExtra(Intent.EXTRA_TEXT, url)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            activity.startActivity(Intent.createChooser(intent, "share"))
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun sysCallShareFB(activity: Activity, url: String) {
        try {
            val comp = ComponentName(
                "com.facebook.katana",
                "com.facebook.composer.shareintent.ImplicitShareIntentHandlerDefaultAliasCustomization"
            )
            val intent = Intent(Intent.ACTION_SEND)
            intent.component = comp
            intent.type = "text/plain"
            //intent.putExtra("com.facebook.platform.extra.APPLICATION_ID", activity.getString(R.string.facebook_app_id));
            intent.putExtra(Intent.EXTRA_SUBJECT, "")
            intent.putExtra(Intent.EXTRA_TEXT, url)
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            activity.startActivity(intent)

        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("FDFAFDFAf", "ASDFAF")
            fbAPIShare(activity, url)

        }
    }
}